package com.sita.bluetactivity.TripFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.malinskiy.superrecyclerview.OnMoreListener;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.sita.bluetactivity.Base.BaseApplication;
import com.sita.bluetactivity.Base.SpUtils;
import com.sita.bluetactivity.R;
import com.sita.bluetactivity.http.RestClient;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/10/8.
 */

public class HistoryDataFragment extends Fragment implements TripListener,SwipeRefreshLayout.OnRefreshListener,OnMoreListener{
    private View view;
    private SuperRecyclerView tripList;
    private TripBackBean backBean;
    private VehicleTripAdapter adapter;
    private SwipeRefreshLayout refreshLayout;
    private int numbers=0;
    private static String nowVehicle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_history,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }
    private void initData(){


    }



    @Override
    public void onStart() {
        super.onStart();

        nowVehicle = SpUtils.getString("nowVehicle",null, BaseApplication.getContext());
      //  Log.e("sssxxxxxx",nowVehicle);
        getTripData(5,0);
    }

    private void MakeTrip(){
        MakeTripRequest request = new MakeTripRequest();
        request.controllerId =nowVehicle;
        RestClient.getRestService().makeTrip(request, new Callback<MakeTripBackBean>() {
            @Override
            public void success(MakeTripBackBean makeTripBackBean, Response response) {
                //TODO
                if (makeTripBackBean.getErrorCode().equals("SUCCESS")){
                  getTripData(5,0);
                  }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }
    private void getTripData(int pagesize,int number){
        TripRequest request = new TripRequest();
        request.controllerId =nowVehicle;
        request.pagesize = pagesize;
        request.pagenumber =number;
        RestClient.getRestService().getVehicleTrip(request, new Callback<TripBackBean>() {
            @Override
            public void success(TripBackBean tripBackBean, Response response) {
                if (tripBackBean.getErrorCode().equals("SUCCESS")){
                    backBean = tripBackBean;
                    adapter.setData(backBean);
                }
            }
            @Override
            public void failure(RetrofitError error) {
            }
        });
    }
    private void initView(){
        tripList = (SuperRecyclerView) getActivity().findViewById(R.id.drive_data_list);
        adapter = new VehicleTripAdapter(getActivity(),this);
        tripList.setLayoutManager(new LinearLayoutManager(getActivity()));
        tripList.setAdapter(adapter);
        refreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.refresh_layout);
        tripList.setRefreshListener(this);

    }

    @Override
    public void AllTripClick(int position) {

        Log.e("222222",backBean.getData().get(position).getRoutefile());
       TripMapActivity.jumpTripActivity(getActivity(),backBean.getData().get(position).getRoutefile(),
               backBean.getData().get(position).getTripId(),
               backBean.getData().get(position).getSpeed(),
               backBean.getData().get(position).getSoc(),
               backBean.getData().get(position).getDistance());
    }

    @Override
    public void onRefresh() {
        getTripData(10,0);
    }

    @Override
    public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
        numbers+=1;
        getTripData(5,numbers);

    }
}
